// 题型

const {
    getById,    // 根据id获取题型
    getList,    // 获取题型列表
    create,     // 添加题型
    edit,       // 编辑题型
    remove,     // 删除题型
    getQuestionList // 查看所有提列表
} = require("../../service/question/questiontype")

exports.questionTypeApi = async ctx => {
    // 获取url 字段 继而判断接下来操作
    let url = ctx.request.url.split("/");
    let urlKey = url[2].split("?")[0];
    const { id } = ctx.params || "";
    const { pageNum, pageSize } = ctx.query || ""
    const { title } = ctx.request.body || "";
    let res;
    let arr = ctx.request.url.split("?")
    switch (ctx.request.method) {
        case "GET":
            if (ctx.request.url.split("?")[0] === "/question") {
                res = await getQuestionList({ pageNum, pageSize })
            } else {
                if (urlKey == "byid") {
                    res = await getById({ id, pageNum, pageSize })
                    // res = "asdf"
                } else if(urlKey == "list") {
                    res = await getList({ pageNum, pageSize })
                }
            }; break;
        case "POST": res = await create({ title }); break;
        case "DELETE": res = await remove({ id }); break;
        case "PUT": res = await edit({ id, title }); break;
        default: res = { msg: "操作失败", code: "400" }
    }
    ctx.body = res
}